// pages/shop/shop.js
const t = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    iconsUrl:t.globalData.iconsUrl,
    accountIndex:0,
    showbox1:false,
    shopname:'',
    shops:true,
    select1:1,
    select2:1,
    select3:4,
    shoparr: [ // value传给后台的值 ， label页面上显示的文字
      {value:'0', label:'全部门店'},
    ],
    shops:false,
    loading:true,
    user_id:'',
    b_id:0,
    u_name:'',
    defaultImg:'/images/bce5.jpg',
    imgList:[
      '',
      '',
      '',
      '',
      ''
    ],
    store_img_one:'',
    store_img_two:'',
    store_img_three:'',
    store_img_four:'',
    store_img_five:'',
    sites:[],
    introduce:'',
    led_introduce:'',
    first_min:0,
    first_price:0,
    next_price:0,
    tel:'',
    recharge1:0,
    recharge2:0,
    recharge3:0,
    recharge4:0,
    recharge5:0,
    recharge6:0,
    give1:0,
    give2:0,
    give3:0,
    give4:0,
    give5:0,
    give6:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.cklogin();
    this.get_shops();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onRefresh(){
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading(); 
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    that.get_shops();
  },
  onPullDownRefresh: function () {
    //调用刷新时将执行的方法
    this.onRefresh();
    setTimeout(function () {
      // 不加这个方法真机下拉会一直处于刷新状态，无法复位
      wx.stopPullDownRefresh()
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  get_storeManage(){
    let that=this;
    t.request('/Merchant/MerchantSide/storeManage',{user_id:that.data.user_id,shop_id:that.data.b_id},function(e){
      let sites=e.result.sites;
      for(let i=0;i<sites.length;i++){
        if(sites[i].status==1){
          sites[i].newstatus='site3';
        }else if(sites[i].working==1){
          sites[i].newstatus='site1';
        }else{
          sites[i].newstatus='site2';
        }
      }
      that.setData({
        sites:e.result.sites,
        first_min:e.result.first_min,
        first_price:e.result.first_price,
        next_price:e.result.next_price,
        tel:e.result.tel,
        introduce:e.result.introduce,
        led_introduce:e.result.led_introduce,
        store_img_one:e.result.store_img_one,
        store_img_two:e.result.store_img_two,
        store_img_three:e.result.store_img_three,
        store_img_four:e.result.store_img_four,
        store_img_five:e.result.store_img_five,
      })
      let recharge=e.result.recharge_config;
      let j=1;
      for(let i=0;i<recharge.length;i++){
        that.setData({
          ['recharge'+j]:recharge[i].real_pay,
          ['give'+j]:recharge[i].prize,
        });
        console.log(i);
        j++;
      }
      console.log(recharge);
    })
  },
  dosave1(){
    let that=this;
    let introduce=that.data.introduce;
    t.request('/Merchant/MerchantSide/storeUpdata',{user_id:that.data.user_id,shop_id:that.data.b_id,introduce:introduce,store_img_one:that.data.store_img_one,store_img_two:that.data.store_img_two,store_img_three:that.data.store_img_three,store_img_four:that.data.store_img_four,store_img_five:that.data.store_img_five},function(e){
      let list =e.result;
      wx.showToast({
        title: '修改成功',
      })
      that.get_shops();
    })
  },
  // led屏公告
  dosave3(){
    let that=this;
    let led_introduce=that.data.led_introduce;
    t.request('/Merchant/MerchantSide/ledUpdata',{shop_id:that.data.b_id,led_introduce:led_introduce,},function(e){
      wx.showToast({
        title: '修改成功',
      })
      that.get_shops();
    })
  },
  dosave2(){
    let that=this;
    let first_min=that.data.first_min;
    let first_price=that.data.first_price;
    let next_price=that.data.next_price;
    let tel=that.data.tel;
    let recharge1=that.data.recharge1;
    let recharge2=that.data.recharge2;
    let recharge3=that.data.recharge3;
    let recharge4=that.data.recharge4;
    let recharge5=that.data.recharge5;
    let recharge6=that.data.recharge6;
    let give1=that.data.give1;
    let give2=that.data.give2;
    let give3=that.data.give3;
    let give4=that.data.give4;
    let give5=that.data.give5;
    let give6=that.data.give6;
    t.request('/Merchant/MerchantSide/storeUpdata',{user_id:that.data.user_id,shop_id:that.data.b_id,first_min:first_min,first_price:first_price,next_price:next_price,tel:tel,},function(e){
      let list =e.result;
    })
    t.request('/Merchant/MerchantSide/changeRecharge',{user_id:that.data.user_id,shop_id:that.data.b_id,recharge1:recharge1,recharge2:recharge2,recharge3:recharge3,recharge4:recharge4,recharge5:recharge5,recharge6:recharge6,give1:give1,give2:give2,give3:give3,give4:give4,give5:give5,give6:give6},function(e){
      let list =e.result;
    })
    wx.showToast({
      title: '修改成功',
    })
    that.get_shops();
  },
  stie_action(e){
    let that=this;
    let id=e.currentTarget.dataset.id;
    let type=e.currentTarget.dataset.index;
    t.request('/Merchant/MerchantSide/stieAction',{site_id:id,shop_id:that.data.b_id,type:type},function(e){
      wx.hideLoading({success: (res) => {},})
      if (e.code==200) {
        wx.showToast({
          title: e.msg,
        })
        that.get_shops();
      }else{
        wx.showToast({
          title: e.msg,
          icon:'none'
        })
      }
    })
    
  },


  cklogin(){
    let user_id=wx.getStorageSync('user_id');
    if(user_id){
      let user=wx.getStorageSync('user');
      this.setData({
        user_id:user_id,
        u_name: user.u_name
       })
    }else{
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },
  get_shops(){
    var that = this
    if(wx.getStorageSync('user_id')){
      t.request('/Merchant/MerchantSide/storeList',{user_id:that.data.user_id},function(e){
        let list=e.result.list;
        that.setData({
          accountIndex:t.globalData.accountIndex,
        })
        if(list.length>1){//多店铺
          let gid=t.globalData.b_id;
          if(gid!=0){
            that.setData({
              b_id:gid,
            })
          }
          let shoparr=[{value:'0', label:'全部门店'},];
          for(let i=0;i<list.length;i++){
            let arr ={};
            arr['value']=list[i].store_id;
            arr['label']=list[i].title;
            shoparr.push(arr);
          }
          console.log(shoparr);
          that.setData({
            shops:true,
            shoparr:shoparr
          })
        }else{
          that.setData({
            shopname:e.result.list[0].title,
            b_id:e.result.list[0].store_id,
          })
        }
        that.get_storeManage();
      })
    }
  },
  bindTextAreaBlur(e) {
    this.setData({
      introduce:e.detail.value,
    })
  },
  bindTextAreaBlurLed(e) {
    console.log(e.detail.value);
    this.setData({
      led_introduce:e.detail.value,
    })
  },
  inputchange1(e){
    this.setData({
      first_min:e.detail.value,
    })
  },
  inputchange2(e){
    this.setData({
      first_price:e.detail.value,
    })
  },
  inputchange3(e){
    this.setData({
      next_price:e.detail.value,
    })
  },
  inputchange4(e){
    this.setData({
      tel:e.detail.value,
    })
  },
  inputrecharge1(e){
    this.setData({
      recharge1:e.detail.value,
    })
  },
  inputrecharge2(e){
    this.setData({
      recharge2:e.detail.value,
    })
  },
  inputrecharge3(e){
    this.setData({
      recharge3:e.detail.value,
    })
  },
  inputrecharge4(e){
    this.setData({
      recharge4:e.detail.value,
    })
  },
  inputrecharge5(e){
    this.setData({
      recharge5:e.detail.value,
    })
  },
  inputrecharge6(e){
    this.setData({
      recharge6:e.detail.value,
    })
  },
  inputgive1(e){
    this.setData({
      give1:e.detail.value,
    })
  },
  inputgive2(e){
    this.setData({
      give2:e.detail.value,
    })
  },
  inputgive3(e){
    this.setData({
      give3:e.detail.value,
    })
  },
  inputgive4(e){
    this.setData({
      give4:e.detail.value,
    })
  },
  inputgive5(e){
    this.setData({
      give5:e.detail.value,
    })
  },
  inputgive6(e){
    this.setData({
      give6:e.detail.value,
    })
  },
  onRefresh(){
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading(); 
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    this.get_shops();
  },
  onPullDownRefresh: function () {
    //调用刷新时将执行的方法
    this.onRefresh();
    setTimeout(function () {
      // 不加这个方法真机下拉会一直处于刷新状态，无法复位
      wx.stopPullDownRefresh()
    }, 1000)
  },
  navigate(e){
    let page=e.currentTarget.dataset.page
    if (page == 1) {
      wx.redirectTo({
        url: '/pages/index/index',
      })
    }else if (page == 2) {
      wx.redirectTo({
        url: '/pages/info/info',
      })
    }else if(page == 3){
      wx.redirectTo({
        url: '/pages/record/record',
      })
    }else if(page == 4){
      wx.redirectTo({
        url: '/pages/shop/shop',
      })
    }
  },
  bindAccountChange(e){
    console.log('切换改变的值', e.detail.value);
    let bid=this.data.shoparr[e.detail.value];
    console.log(bid.value);
    t.globalData.b_id=bid.value;
    t.globalData.accountIndex=e.detail.value;
    this.setData({
      b_id: bid.value,
      accountIndex:e.detail.value
    })
    this.get_shops();
  },
  showbox1(e){
    this.setData({
      showbox1: true
    })
    setTimeout(()=>
    {
      this.setData({
        showbox1: false
      })
    }, 2000)
  },
  loginout(e){
    this.setData({
      showbox1: false
    })
    wx.removeStorageSync("user");
    wx.removeStorageSync("user_id");
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  uploadPhoto(e) {
    let index=e.currentTarget.dataset.index;
      var that = this; 
      wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths;
          if (res.tempFiles[0].size > 1024 * 201) {
            wx.showToast({
                title: '图片请控制在200KB以内', //标题
                icon: 'none' //图标 none不使用图标，详情看官方文档
            })
            return;
          }else{
            that.upload(that, tempFilePaths,index);
          }
          /*var imgList = that.data.imgList
          imgList[index] = tempFilePaths;
          that.setData({
            imgList: imgList
          })*/
        
        }
      })
  },
  upload(page, path,index) {
    wx.showToast({
      icon: "loading",
      title: "正在上传"
    })
    var url=t.globalData.url+'/Merchant/MerchantSide/cosUpload';
    console.log(url);
    wx.uploadFile({
      url: url,
      filePath: path[0], 
      name: 'file',
      header: { "Content-Type": "multipart/form-data" },
      formData: {
      },
      success: function (res) {
        console.log(res);
        if (res.statusCode != 200) { 
          wx.showModal({
            title: '提示',
            content: '上传失败',
            showCancel: false
          })
          return;
        }
        console.log(index);
        let data2 =JSON.parse(res.data);
        /*console.log(data2.result.url);
        page.setData({  //上传成功修改显示头像
          [index]:data2.result.url
        })*/
        var data3 = {};
        data3[index] = data2.result.url; // key 可以是任何字符串
        page.setData(data3);
      },
      fail: function (e) {
        console.log(e);
        wx.showModal({
          title: '提示',
          content: '上传失败',
          showCancel: false
        })
      },
      complete: function () {
        wx.hideToast();  //隐藏Toast
      }
    })
  }
  
})
 